package com.luojialong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luojialong.enums.AffairStatus;
import com.luojialong.exception.AffairException;
import com.luojialong.model.domain.ProjectTb;
import com.luojialong.model.domain.ScoreTb;
import com.luojialong.model.request.scoreTb.ScoreQueryRequest;
import com.luojialong.model.request.scoreTb.ScoreTbAddRequest;
import com.luojialong.model.vo.scoreTb.ScoreTbInfoVo;
import com.luojialong.service.ProjectTbService;
import com.luojialong.service.ScoreTbService;
import com.luojialong.mapper.ScoreTbMapper;
import com.luojialong.utils.MyStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【score_tb】的数据库操作Service实现
 * @createDate 2024-04-23 20:21:06
 */
@Service
public class ScoreTbServiceImpl extends ServiceImpl<ScoreTbMapper, ScoreTb>
        implements ScoreTbService {


    @Resource
    private ProjectTbService projectTbService;

    @Override
    public long add(ScoreTbAddRequest scoreTbAddRequest) {
        Integer teachgrade = scoreTbAddRequest.getTeachgrade();
        Integer implementGrade = scoreTbAddRequest.getImplementGrade();
        Integer effectGrade = scoreTbAddRequest.getEffectGrade();
        Integer devGrade = scoreTbAddRequest.getDevGrade();
        String description = scoreTbAddRequest.getDescription();
        Integer sumScore = scoreTbAddRequest.getSumScore();
        Long projectId = scoreTbAddRequest.getProjectId();
        Long userId = scoreTbAddRequest.getUserId();
        if (teachgrade == null || implementGrade == null || effectGrade == null || devGrade == null || !StringUtils.isNotBlank(description)
                || sumScore == null || projectId == null || userId == null) {
            throw new AffairException(AffairStatus.PARAMS_ERROR, "前端处理的参数有误");
        }
        ProjectTb projectTb = projectTbService.getById(projectId);
        if (projectTb == null) {
            throw new AffairException(AffairStatus.FAILURE,"该项目已经不存在了");
        }
        if (projectTb.getIsStore() == 1) {
            throw new AffairException(AffairStatus.FAILURE,"已经打过分了,不用在重复打了");
        }
        ScoreTb addScoreTb = new ScoreTb();
        BeanUtils.copyProperties(scoreTbAddRequest, addScoreTb);
        boolean save = this.save(addScoreTb);
        if (!save) {
            throw new AffairException(AffairStatus.FAILURE, "评分表没有保存成功");
        }
        return addScoreTb.getId();
    }

    @Override
    public QueryWrapper<ScoreTb> getQueryWrapper(ScoreQueryRequest scoreQueryRequest) {
        String sortField = scoreQueryRequest.getSortField();
        String sortOrder = scoreQueryRequest.getSortOrder();

        QueryWrapper<ScoreTb> queryWrapper = new QueryWrapper<>();
        Long id = scoreQueryRequest.getId();
        Long projectId = scoreQueryRequest.getProjectId();
        Long userId = scoreQueryRequest.getUserId();
        if (id != null) {
            queryWrapper.eq("id", id);
        } else if (projectId != null) {
            queryWrapper.eq("projectId", projectId);
        } else if (userId != null) {
            queryWrapper.eq("userId", userId);
        }
        MyStringUtils.setWrapper(queryWrapper, sortField, sortOrder);
        return queryWrapper;
    }

    @Override
    public Page<ScoreTbInfoVo> getVo(Page<ScoreTb> scoreTbPage) {
        List<ScoreTb> records = scoreTbPage.getRecords();
        List<ScoreTbInfoVo> collect = records.stream().map(ScoreTbInfoVo::transferVo).collect(Collectors.toList());
        Page<ScoreTbInfoVo> scoreTbInfoVoPage = new Page<>(scoreTbPage.getCurrent(),scoreTbPage.getSize(),scoreTbPage.getTotal());
        scoreTbInfoVoPage.setRecords(collect);
        return scoreTbInfoVoPage;
    }

    @Override
    public Boolean del(Long id) {
        ProjectTb projectTb = projectTbService.getById(id);
        projectTb.setIsStore(0);
        boolean b = projectTbService.updateById(projectTb);
        if (!b) {
            throw new AffairException(AffairStatus.FAILURE,"数据没有更新成功");
        }
        boolean res = this.removeById(id);
        return res;
    }
}




